package com.wanglei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanglei.domain.ResponseResult;
import com.wanglei.domain.dto.TagListDto;
import com.wanglei.domain.entity.Tag;
import com.wanglei.domain.vo.PageVo;
import com.wanglei.domain.vo.TagVo;
import com.wanglei.service.TagService;
import com.wanglei.mapper.TagMapper;
import com.wanglei.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author MrWang
* @description 针对表【sg_tag(标签)】的数据库操作Service实现
* @createDate 2023-07-09 16:31:50
*/
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag>
    implements TagService{

    @Autowired
    private TagMapper tagMapper;

    @Override
    public ResponseResult list(Integer pageNum, Integer pageSize, String name) {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<Tag>()
                .like(StringUtils.hasText(name),Tag::getName,name);//模糊查询
        Page<Tag> page = new Page<>(pageNum,pageSize);
        tagMapper.selectPage(page,queryWrapper);
        List<Tag> tags = page.getRecords();
        //vo
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(tags, TagVo.class);

        return ResponseResult.okResult(new PageVo(tagVos,page.getTotal()));
    }

    @Override
    public ResponseResult addTag(TagListDto tagListDto) {
        Tag tag = BeanCopyUtils.copyBean(tagListDto, Tag.class);
        tagMapper.insert(tag);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteTag(Integer id) {
        tagMapper.deleteById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getTagById(Integer id) {
        Tag tag = tagMapper.selectById(id);
        TagVo tagVo = BeanCopyUtils.copyBean(tag, TagVo.class);
        return ResponseResult.okResult(tagVo);
    }

    @Override
    public ResponseResult updateTag(Tag tag) {
        tagMapper.updateById(tag);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult listAllTag() {
        List<Tag> tags = tagMapper.selectList(null);
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(tags, TagVo.class);
        return ResponseResult.okResult(tagVos);
    }

}




